﻿using ECGadmin.Common;
using ECGadmin.DAL;
using ECGadmin.WebUI.Attributes;
using ECGadmin.WebUI.CustomHelpers;
using ECGadmin.WebUI.Models;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ECGadmin.WebUI.Controllers
{
    [AdminLoggedAuthorize]
    public class ProductHotController : Controller
    {
        //
        // GET: /ProductHot/

        //public ActionResult Index(int page = 1)
        //{
        //    var totalPage = 0;
        //    var itemsPerPage = int.Parse(ConfigurationManager.AppSettings["AdminProductListPageSize"]);
        //    ViewBag.Page = page;
        //    var model = ProductDb.GetAllProductHotPaging(page, itemsPerPage, ref totalPage) ?? new List<ProductListForPagingDto>();
        //    ViewBag.PageInfo = new PageInfo { CurrentPage = page, PageSize = itemsPerPage, TotalPages = totalPage };
        //    ViewBag.MenuList = FunctionHelper.GetMenuListThreeLevel() ?? new List<ProductCategoryDto>();

        //    return View(model);
        //}

        public ActionResult Add(int page = 1)
        {
            ViewBag.MenuList = FunctionHelper.GetMenuListThreeLevel() ?? new List<ProductCategoryDto>();
            return View();
        }

        public PartialViewResult AjaxProductHotByCate(string categoryId)
        {
            var page = 1;
            var totalPage = 0;
            var itemsPerPage = 1000;
            ViewBag.Page = page;
            var model = ProductDb.GetAllProductByCategoryId(page, itemsPerPage, ref totalPage, categoryId) ?? new List<ProductByCateTopListPagingDto>();
            return PartialView(model);
        }
        public JsonResult AddProductHot(string ListMap)
        {
            var arr = ListMap.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            if (arr.Length > 0)
            {
                foreach (var mapId in arr)
                {
                    //ProductTogetherDb.AddProductTogether(ProductId, mapId, true);
                    var model = new ProductHotDto
                    {
                        ProductId = mapId.Split(',')[0],
                        EndDate = !string.IsNullOrEmpty(mapId.Split(',')[1]) ? Convert.ToDateTime(mapId.Split(',')[1]) : DateTime.Now,
                        Show = Convert.ToBoolean(mapId.Split(',')[2])
                    };
                    ProductHotDb.AddProductHot(model);
                }
            }
            return Json(true, JsonRequestBehavior.AllowGet);
        }

        public PartialViewResult GetListProductHot()
        {
            var model = ProductHotDb.GetListAllProductHot();
            return PartialView(model);
        }

        public JsonResult DelProductHot(int Id)
        {
            ProductHotDb.Delete(Id);
            return Json(true, JsonRequestBehavior.AllowGet);
        }
        public JsonResult DelAllProductHot()
        {
            ProductHotDb.DelAllProductHot();
            return Json(true, JsonRequestBehavior.AllowGet);
        }

    }
}
